package com.yunji.base.dao.impl;

import com.yunji.base.dao.ISiteDao;
import com.yunji.base.dto.SiteFieldDTO;
import com.yunji.base.mapper.ISiteMapper;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.model.domain.SiteDO;
import com.yunji.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : peter-zhu
 * @date : 2024/12/11 17:47
 * @description : TODO
 **/
@Repository
public class SiteDaoImpl extends BaseDaoImpl<ISiteMapper, SiteDO> implements ISiteDao {

    @Resource
    ISiteMapper siteMapper;

    @Override
    public SiteDO getByBelongId(Long belongId) {
        return lambdaQuery().eq(SiteDO::getBelongId, belongId).one();
    }

    @Override
    public boolean existSiteName(SiteDO siteName) {
        return lambdaQuery().eq(SiteDO::getSiteName, siteName.getSiteName()).ne(BeanUtils.isNotNull(siteName.getId()), SiteDO::getId,siteName.getId()).exists();
    }


    @Override
    public List<String> searchTableFiled(SiteFieldDTO param) {
        return this.baseMapper.searchTableFiled(param);
    }

//    SELECT sit.id, sit.site_name AS name FROM  t_site sit
//    LEFT JOIN v3_belong b ON b.belong_id = sit.belong_id
//    LEFT JOIN  v3_belong p ON p.belong_id = b.parent_id
//    WHERE sit.del_flag = 0
//        <if test="siteTypeIds != null and siteTypeIds.length > 0">
//            <foreach collection="siteTypeIds" item="siteType" open="AND  sit.site_type IN(" separator="," close=")">
//            #{siteType}
//            </foreach>
//        </if>
//        <if test="agentIds != null and agentIds.length > 0">
//            <foreach collection="agentIds" item="agentId" open="AND p.agent_id  IN(" separator="," close=")">
//            #{agentId}
//            </foreach>
//        </if>
//        <if test="scaleType != null">AND b.scale_type = #{scaleType}</if>
//        <if test="parentId != null">AND p.belong_id = #{parentId}</if>

//    @Override
//    public List<LinkageQueryVO> siteLinkageQuery(SiteLinkageQueryDTO param) {
//        MPJLambdaWrapper<Site> wrapper = new MPJLambdaWrapper<>();
//        MPJLambdaWrapper<Site> cd = wrapper.select(Site::getId).selectAs(Site::getSiteName, "name")
//                .leftJoin(Belong.class, "b", Belong::getBelongId, Site::getBelongId)
//                .leftJoin(Belong.class, "p", Belong::getBelongId, Belong::getParentId)
//                .in(param.getAgentIds() != null && param.getAgentIds().length > 0,
//                        MybatisPlusHelper.aliasCondition(Belong::getAgentId, "p"), Arrays.asList(param.getAgentIds()))
//                .in(param.getSiteTypeIds() != null && param.getSiteTypeIds().length > 0,
//                        Site::getSiteType, Arrays.asList(param.getSiteTypeIds()))
//                .eq(param.getScaleType() != null, MybatisPlusHelper.aliasCondition(Belong::getScaleType, "b"),
//                        param.getScaleType())
//                .eq(param.getParentId() != null, MybatisPlusHelper.aliasCondition(Belong::getParentId, "p"),
//                        param.getParentId());
//        return selectJoinList(LinkageQueryVO.class, cd);
//    }
}
